package com.htfyun.eink.pw;

import android.view.View;

import androidx.annotation.NonNull;

import com.htfyun.eink.pw.configs.PWConfig;
import com.htfyun.eink.pw.core.EinkHelper;
import com.htfyun.eink.pw.core.manager.EinkManagerInstance;
import com.htfyun.eink.pw.draw.IPWDraw;
import com.htfyun.eink.pw.draw.pen.PenRandomEraser;
import com.htfyun.eink.pw.draw.pen.PenRandomEraserPath;
import com.htfyun.eink.pw.draw.pen.PenRandomFixWidth;
import com.htfyun.eink.pw.draw.pen.PenShapeCircle;
import com.htfyun.eink.pw.draw.pen.epd.PenEPDBrush;
import com.htfyun.eink.pw.draw.pen.epd.PenEPDCharcoal;
import com.htfyun.eink.pw.draw.pen.epd.PenEPDFountain;
import com.htfyun.eink.pw.draw.pen.epd.PenEPDMarker;

public class PWHelper {

    public static final PWConfig pwConfig = PWConfig.Instance;

    /**
     * 维护 1 张 bmp, 和对应的 canvas :<br>
     * <p>1. pw bmp: {@link IPWDraw#getBitmapOfPW()}, 用于手写 bmp.<br>
     *
     * @param view 绑定 view
     */
    public static void bindView(@NonNull View view) {

        /*
        20230219: 目前在PX30s 上测试:
         A4+GLD: 在手写刷新时, 无锯齿. 会'闪'
         A2+GLD: 在手写刷新时, 有锯齿. 不会'闪'
         */
        EinkHelper.setEinkUpdateModeWithView(view,
                pwConfig.configFreshMode.getViewDataMode(),
                pwConfig.configFreshMode.getViewShowMode());

        EinkManagerInstance.getInstance().initManager(view.getContext());

        pwConfig.pwDraw.bindView(view);

        pwConfig.pwDraw.registerPenWriter(new PenRandomFixWidth());
        pwConfig.pwDraw.registerPenWriter(new PenShapeCircle());
        pwConfig.pwDraw.registerPenWriter(new PenEPDBrush());
        pwConfig.pwDraw.registerPenWriter(new PenEPDFountain());
        pwConfig.pwDraw.registerPenWriter(new PenEPDMarker());
        pwConfig.pwDraw.registerPenWriter(new PenEPDCharcoal());
        pwConfig.configPen.setWritePenId(PenRandomFixWidth.PEN_ID);
//        pwConfig.configPen.setWritePenId(PenShapeCircle.PEN_ID);

        pwConfig.pwDraw.registerPenEraser(new PenRandomEraser());
        pwConfig.pwDraw.registerPenEraser(new PenRandomEraserPath());

    }

    /**
     * 解绑 view. 但是不会 清掉 bmp 的内存. <br>
     * 如果要清掉 bmp 的内存, 需要调用 {@link #clearMemory()}
     *
     * @param view 解绑 view
     */
    public static void unbindView(@NonNull View view) {
        pwConfig.pwDraw.unbindView(view);
    }

    /**
     * 清掉内存. 就是清掉 bmp 的内存.
     */
    public static void clearMemory() {
        pwConfig.pwDraw.clearMemory();
    }
}
